<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="Public/Admin/css/bootstrap.min.css" />
	<link rel="stylesheet" href="Public/Admin/css/common.css" />
	<script type="text/javascript" src='Public/Admin/js/jquery-1.8.2.min.js'></script>
	<style type="text/css">
		body{
			margin-bottom: 100px;
		}
	</style>
	<script type="text/javascript">
		$(function(){
//			异步传输表单到php然后执行添加
//给表单绑定表单提交事件
			$('#sub').submit(function(){
				var filedata=$(this).serialize();
				$.ajax({
					type:"post",
					url:"<?php echo U('Product/ajaxadd')?>",
					data:filedata,
					dataType:'json',
					success:function(phpdata){
						var at='';
						if(phpdata){			
 						//定义一个空字符串						
									at+='<tr class="endtr">'+
											'<td>'+phpdata.plid+'</td>';
											$.each(phpdata.connectid, function(nn,ii) {
												at+='<td>'+ii+'</td>';
											});
									at+='<td>'+phpdata.plstock+'</td>'+
										'<td>'+phpdata.plorder+'</td>'+							
										'<td>'+
											'<a plid="'+phpdata.plid+'" href="index.php?m=admin&c=product&a=edit&gid='+phpdata.goods_gid+ '&plid='+phpdata.plid+'"class="btn btn-warning edit"><i class="icon-pencil icon-white"></i>修改</a>'+
											'<a plid="'+phpdata.plid+'" href="javascript:;" class="btn btn-danger del"><i class="icon-pencil icon-danger"></i>删除</a>'+
										'</td></tr>';
							
								
						}else{
							alert('信息输入有误或者货品列表已经有该物品');
						}
						$('table .endtr:last').after(at);
						
					}
				});
				//return false是组织表单提交，这样就不会刷新也页面
				return false;
			})
			
			//异步删除对应
			$('.del').live('click',function(){
				 var plid=$(this).attr('plid');
				//异步ajax传送来删除
				if(confirm('确认删除吗？')){
					
								
					$.ajax({
						type:"post",
						url:"<?php echo U('Product/ajaxdel')?>",
						data:{plid:plid},
						dataType:'json',
						success:function(phpdata){
							if(phpdata){																			
							}
						}
					});
					
					$(this).parents('.endtr').remove();
				}
			})
			//点击修改按钮跳转到对应的控制器和模板也
			
			
		})
	</script>
</head>
<body>
	<div class="container-fluid">
	<div class="row-fluid">
	<div class="span12">

	
	<form id="sub" action="" method='post'>

		<table class='table table-bordered table-hover'>
			<tr class="endtr">
				<th>货品ID</th>
				<?php foreach ($arr as $v){?>
				<th width="100"><?php echo $v[0]['taname']?></th>
				<?php }?>
				<th>库存</th>
				<th>货号</th>
				<th>操作</th>
			</tr>
			<?php foreach ($goodlistdata as $v){?>
			<tr class="endtr">
				<th><?php echo $v['plid']?></th>
				<?php foreach ($v['connectid'] as $vv){?>
				<th width="100"><?php echo $vv?></th>
				<?php }?>	
				<th><?php echo $v['plstock']?></th>
				<th><?php echo $v['plorder']?></th>
				<th>
					<a plid="<?php echo $v['plid']?>"  href="<?php echo U('product/edit',array('gid'=>Q('get.gid'),'plid'=>$v['plid']))?>" class="btn btn-warning edit"><i class="icon-pencil icon-white"></i>修改</a>
					<a plid="<?php echo $v['plid']?>" href="javascript:;" class="btn btn-danger del"><i class="icon-pencil icon-danger"></i>删除</a>
				
				</th>
			</tr>
			<?php }?>
			

			<!-- 如果已添加数目小于可能情况数目，才出现添加货品的输入框 -->
		
			<tr class="info">
				<td>添加货品</td>
		
				<?php foreach ($arr as $k=>$v){?>					
					<td>
						<select name="spec[]">
							<option value="">-请选择-</option>
							<?php foreach ($v as $vv){?>
								<option value="<?php echo $vv['gaid']?>"><?php echo $vv['gavalue']?></option>
							<?php }?>
						</select>
					</td>
				<?php }?>
				<td>
					<input type="text" name='plstock'/>
				</td>
				<td>
					<input type="text" name='plorder' />
				</td>
				<td></td>
			</tr>
			
		</table>

	
		<input type="hidden" name='goods_gid' value="<?php echo Q('get.gid')?>"/>		
		<input type="submit" value='保存添加' class="btn btn-primary"/>
	</form>
	

	</div>
	</div>
	</div>


<!--<script type="text/javascript">
	var sel = $('select[name="spec[]"]');
	var len = sel.length;
	sel.change(function () {
		var remote = {
			'gid' : {$Think.get.gid},
			'spec' : {}
		};
		for (var i = 0; i < len; ++i)
		{
			if (sel.eq(i).val() == 0)
			{
				return;
			}
			else
			{
				remote.spec[i] = sel.eq(i).val();
			}
		}
		$.ajax({
			url : '{:U("check")}',
			type : 'get',
			data : remote,
			dataType : 'json',
			success : function(data)
			{
				if (data == 0)
				{
					alert('货品已存在，如果要修改库存，请点击修改');
					for (var i = 0; i < len; ++i)
					{
						sel.eq(i).val(0);
					}
				}
			}
		});
	});
</script>-->
</body>
</html>